<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller 
{
    public function add()
    {
        //如果会员没有登录则跳到登陆页,登录成功再跳回来
        $memberId = session('m_id');
        if(!$memberId){
            session('returnUrl',U('Order/add'));
            redirect(U('Member/login'));
        }
    	if(IS_POST)
    	{
    		$orderModel = D('Admin/Order');
    		if($orderModel->create(I('post.'), 1))
    		{
    			if($orderId = $orderModel->add())
    			{
    				$this->success('下单成功！', U('order_success?order_id='.$orderId));
    				exit;
    			}
    		}
    		$this->error($orderModel->getError());
    	}
    	//取出购物车中的所有商品
        $cartModel = D('Cart');
        $data = $cartModel->cartList();

        //取出默认收货地址
        $addressModel = D('Admin/UserAddress');
        $addressData = $addressModel->relation(true)
            ->where(array(
                'member_id' => array('eq',$memberId),
            ))->order('is_default desc,id desc')
              ->find();

        //取出送货方式
        $shippingModel = M('shipping');
        $shippingData = $shippingModel->where(array(
            'enabled' => array('eq',1)
        ))->order('shipping_order desc,shipping_id asc')
          ->select();

        //取出发票内容
        $inv_content = C('INV_CONTENT');

		// 设置页面中的信息
		$this->assign(array(
		    'data1' => $data,
            'addressData' => $addressData,
            'shippingData' => $shippingData,
            'inv_content' => $inv_content,
			'_page_title' => '订单确认页',
			'_page_keywords' => '订单确认页',
			'_page_description' => '订单确认页',
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Order');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Order');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Order');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('取消订单成功！', U('My/order'));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Admin/Order');
    	$data = $model->search();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '列表',
			'_page_btn_name' => '添加',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }

    //接收支付宝的信息
    public function receive(){
        require('/alipay/notify_url.php');
    }

    public function test(){
        $db = M();
        $rs = $db->query('SELECT id FORM test.a');
        $rs[0]['id']--;
        $db->execute('UPDATE test.a SET id='.$rs[0]['id']);
    }

    public function order_success(){
        $btn = makeAlipayBtn(I('get.order_id'));
        $this->assign(array(
            'btn' => $btn,
            '_page_title' => '下单成功',
            '_page_keywords' => '下单成功',
            '_page_description' => '下单成功',
        ));
        $this->display();
    }

    //支付
    public function pay()
    {
        $orderId = I('get.id');
        $model = D('Admin/Order');
        $data['pay_status'] = '是';
        $data['pay_time'] = time();
        if($model->where('id='.$orderId)->save($data)){
            $this->success('支付成功',U('My/order_detail?order_id='.$orderId));
            exit;
        }
        $this->error($model->getError());

    }
}